<html>
<head><meta charset="utf-8"><title>best game: is this UB? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html">best game: is this UB?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="134029578"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134029578" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134029578">(Sep 15 2018 at 23:13)</a>:</h4>
<p>Should this Rust be valid?</p>
<div class="codehilite"><pre><span></span><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">mem</span><span class="p">;</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">mem</span>::<span class="n">uninitialized</span>::<span class="o">&lt;&amp;</span><span class="p">()</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="n">a</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">iter</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">c</span><span class="p">.</span><span class="n">cloned</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">e</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">d</span><span class="p">.</span><span class="n">next</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">match</span><span class="w"> </span><span class="n">e</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Some</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="nb">None</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="134029627"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134029627" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134029627">(Sep 15 2018 at 23:15)</a>:</h4>
<p>Spoiler:</p>
<div class="codehilite"><pre><span></span>==2711== Conditional jump or move depends on uninitialised value(s)
==2711==    at 0x10E080: md::main (main.rs:10)
==2711==    by 0x10E16F: std::rt::lang_start::{{closure}} (rt.rs:74)
==2711==    by 0x119EF2: {{closure}} (rt.rs:59)
==2711==    by 0x119EF2: _ZN3std9panicking3try7do_call17he206d249d44a142eE.llvm.17113965321911306163 (panicking.rs:310)
==2711==    by 0x126699: __rust_maybe_catch_panic (lib.rs:105)
==2711==    by 0x10EA85: try&lt;i32,closure&gt; (panicking.rs:289)
==2711==    by 0x10EA85: catch_unwind&lt;closure,i32&gt; (panic.rs:392)
==2711==    by 0x10EA85: std::rt::lang_start_internal (rt.rs:58)
==2711==    by 0x10E147: std::rt::lang_start (rt.rs:74)
==2711==    by 0x10E0E9: main (in /tmp/md/target/debug/md)
</pre></div>


<p>Line 10 is <code>Some(_) =&gt; 1,</code></p>



<a name="134029674"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134029674" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134029674">(Sep 15 2018 at 23:16)</a>:</h4>
<p>but removing <code>cloned</code> or changing to <code>mem::uninitialized::&lt;()&gt;</code> stops the Valgrind error</p>



<a name="134029841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134029841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134029841">(Sep 15 2018 at 23:22)</a>:</h4>
<p><span class="user-mention" data-user-id="119009">@eddyb</span> has evidently already looked at the original version of this.</p>



<a name="134029907"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134029907" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134029907">(Sep 15 2018 at 23:24)</a>:</h4>
<p>i guess the underlying question here is: what has to be initialized so you can you check the discriminant of an enum? at minimum the bytes where the discriminant is stored must be initialized. that's why <code>&amp;()</code> vs <code>()</code> makes a difference -- for the reference, the layout optimization puts the discriminant into the (uninit) bytes of the reference</p>



<a name="134029917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134029917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134029917">(Sep 15 2018 at 23:25)</a>:</h4>
<p>but if we want to keep expanding the amount of layout optimizations we do with enum tags, we need to declare this sort of thing UB for all types where we may want this sort of layout optimization</p>



<a name="134029956"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134029956" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134029956">(Sep 15 2018 at 23:26)</a>:</h4>
<p>The original context - <a href="https://stackoverflow.com/questions/52348277/vector-is-empty-after-cloning-struct-with-uninitialized-member/52349655#52349655" target="_blank" title="https://stackoverflow.com/questions/52348277/vector-is-empty-after-cloning-struct-with-uninitialized-member/52349655#52349655">https://stackoverflow.com/questions/52348277/vector-is-empty-after-cloning-struct-with-uninitialized-member/52349655#52349655</a></p>



<a name="134029969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134029969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134029969">(Sep 15 2018 at 23:27)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> so the nullable pointer optimization kicking in makes sense, but why is <code>cloned</code> needed to trigger it?</p>



<a name="134030010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030010">(Sep 15 2018 at 23:28)</a>:</h4>
<p>i have not thought deeply about that because i expect it to be rooted in some uninteresting technicality of the generated code</p>



<a name="134030024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030024">(Sep 15 2018 at 23:29)</a>:</h4>
<p>One tidbit is that the original code used <code>ManuallyDrop</code> and the code worked in 1.28 but fails in 1.29</p>



<a name="134030061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030061">(Sep 15 2018 at 23:30)</a>:</h4>
<p>and <code>ManuallyDrop</code> changed in 1.29</p>



<a name="134030072"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030072" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030072">(Sep 15 2018 at 23:30)</a>:</h4>
<p>So it <em>seems</em> that stdlib change introduced UB in end-user programs</p>



<a name="134030209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030209">(Sep 15 2018 at 23:35)</a>:</h4>
<p>what's the change in 1.29?<br>
but in any case i would argue this was always UB (like most other uses of mem::uninitialized) and the change you speak of just exposed it</p>



<a name="134030217"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030217" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030217">(Sep 15 2018 at 23:35)</a>:</h4>
<p>ManuallyDrop and MaybeUninit are separate for a reason, though admittedly MaybeUninit still isn't a thing (&gt;_&gt;)</p>



<a name="134030262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030262">(Sep 15 2018 at 23:36)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> <a href="https://github.com/rust-lang/rust/commit/591eeff22af299043637e75bb5735c3c65e0c7fe" target="_blank" title="https://github.com/rust-lang/rust/commit/591eeff22af299043637e75bb5735c3c65e0c7fe">https://github.com/rust-lang/rust/commit/591eeff22af299043637e75bb5735c3c65e0c7fe</a></p>



<a name="134030271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030271">(Sep 15 2018 at 23:36)</a>:</h4>
<p>ah, I had suspected that one but didn't think it was old enough to be in 1.29</p>



<a name="134030281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030281">(Sep 15 2018 at 23:37)</a>:</h4>
<blockquote>
<p>MaybeUninit still isn't a thing (&gt;_&gt;)</p>
</blockquote>
<p>It is not ;-)</p>



<a name="134030400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030400">(Sep 15 2018 at 23:40)</a>:</h4>
<p>OTOH I believe one can do the same in stable library code, using unions (and ManuallyDrop b/c unions containing Drop data are bad)</p>



<a name="134030410"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030410" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030410">(Sep 15 2018 at 23:41)</a>:</h4>
<p>cloned is just because before cloned you have Option&lt;&amp;&amp;T&gt;</p>



<a name="134030411"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030411" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030411">(Sep 15 2018 at 23:41)</a>:</h4>
<p>Some(&amp;garbage)</p>



<a name="134030412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030412">(Sep 15 2018 at 23:41)</a>:</h4>
<p>oh right!</p>



<a name="134030413"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030413" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030413">(Sep 15 2018 at 23:41)</a>:</h4>
<p>which is shallowly valid</p>



<a name="134030414"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030414" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030414">(Sep 15 2018 at 23:41)</a>:</h4>
<p>ah, good point</p>



<a name="134030418"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030418" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030418">(Sep 15 2018 at 23:41)</a>:</h4>
<p>you know, we figured this out hours ago on discord</p>



<a name="134030457"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030457" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030457">(Sep 15 2018 at 23:42)</a>:</h4>
<p>Where's my move-out-array iterators :-p</p>



<a name="134030458"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030458" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030458">(Sep 15 2018 at 23:42)</a>:</h4>
<p>somwthing soemtjong frafmented chat platforms</p>



<a name="134030459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030459">(Sep 15 2018 at 23:42)</a>:</h4>
<p>alsp, mobile keyboards apparently</p>



<a name="134030462"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030462" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030462">(Sep 15 2018 at 23:42)</a>:</h4>
<p><span class="user-mention" data-user-id="119009">@eddyb</span> in fact, no, I didn't, because the original poster asked a question on SO bu t didn't post their own answer</p>



<a name="134030463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030463">(Sep 15 2018 at 23:42)</a>:</h4>
<p>leaving it there, unanswered</p>



<a name="134030465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030465">(Sep 15 2018 at 23:43)</a>:</h4>
<p>idk why people ask on SO <em>first</em></p>



<a name="134030472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030472">(Sep 15 2018 at 23:43)</a>:</h4>
<p>and then come to me</p>



<a name="134030475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030475">(Sep 15 2018 at 23:43)</a>:</h4>
<p>only after posting here and submitting an answer did I see that they mentioned they talked to you</p>



<a name="134030479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030479">(Sep 15 2018 at 23:43)</a>:</h4>
<p>anyway you can find the discussion in #compiler on discord</p>



<a name="134030481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030481">(Sep 15 2018 at 23:43)</a>:</h4>
<p><span class="user-mention" data-user-id="119009">@eddyb</span> by that logic, there would never be any questions on SO. Do you not want a front-line defense?</p>



<a name="134030483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030483">(Sep 15 2018 at 23:43)</a>:</h4>
<p>the gist of it is that mem::uninitialized is utterly useless</p>



<a name="134030522"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030522" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030522">(Sep 15 2018 at 23:44)</a>:</h4>
<p>no, I mean, they already were gonna ask me</p>



<a name="134030524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030524">(Sep 15 2018 at 23:44)</a>:</h4>
<p>because they knew I was aware of them and slotmap</p>



<a name="134030526"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030526" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030526">(Sep 15 2018 at 23:44)</a>:</h4>
<p>Why they posted and then immediately asked you, I dunno. You gotta wait for time and thinking for a Q.</p>



<a name="134030528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030528">(Sep 15 2018 at 23:44)</a>:</h4>
<p>like, we had discussed it before lol</p>



<a name="134030540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030540">(Sep 15 2018 at 23:45)</a>:</h4>
<p>anyway, yeah, this is an impossible problem on stable without using space or restricting the types of values significantly</p>



<a name="134030544"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030544" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030544">(Sep 15 2018 at 23:45)</a>:</h4>
<p>and on nightly you have to keep your own copy of MaybeUninit</p>



<a name="134030588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030588">(Sep 15 2018 at 23:46)</a>:</h4>
<p>what why would you need nightly for copying MaybeUninit?</p>



<a name="134030590"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030590" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030590">(Sep 15 2018 at 23:46)</a>:</h4>
<p>copy restriction on unions</p>



<a name="134030591"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030591" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030591">(Sep 15 2018 at 23:46)</a>:</h4>
<p>the OP is the author of slotmap</p>



<a name="134030592"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030592" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030592">(Sep 15 2018 at 23:46)</a>:</h4>
<p>oh it requires Copy not absence of drop glue</p>



<a name="134030597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030597">(Sep 15 2018 at 23:47)</a>:</h4>
<p>yeah the RFC isn't even in FCP yet</p>



<a name="134030698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030698">(Sep 15 2018 at 23:50)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> you should be linking <a href="https://github.com/rust-lang/rust/issues/53491" target="_blank" title="https://github.com/rust-lang/rust/issues/53491">https://github.com/rust-lang/rust/issues/53491</a> or related threads</p>



<a name="134030705"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030705" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030705">(Sep 15 2018 at 23:51)</a>:</h4>
<p>or maybe just <a href="https://github.com/rust-lang/rust/pull/53508" target="_blank" title="https://github.com/rust-lang/rust/pull/53508">https://github.com/rust-lang/rust/pull/53508</a></p>



<a name="134030708"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030708" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030708">(Sep 15 2018 at 23:51)</a>:</h4>
<p><span class="user-mention" data-user-id="119009">@eddyb</span> linking ... from where? in my answer?</p>



<a name="134030711"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030711" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030711">(Sep 15 2018 at 23:51)</a>:</h4>
<p>something in there should explain how useless <code>mem::uninitialized</code> is</p>



<a name="134030712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030712">(Sep 15 2018 at 23:51)</a>:</h4>
<p>yeah, somewhere on the SO post</p>



<a name="134030714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030714">(Sep 15 2018 at 23:51)</a>:</h4>
<p>just in general? Is this my new .sig? <span class="emoji emoji-1f609" title="wink">:wink:</span></p>



<a name="134030716"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030716" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030716">(Sep 15 2018 at 23:51)</a>:</h4>
<p>lol</p>



<a name="134030742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030742">(Sep 15 2018 at 23:52)</a>:</h4>
<p>I just want to go to sleep</p>



<a name="134030759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030759">(Sep 15 2018 at 23:52)</a>:</h4>
<p>point is, this is not an isolated case. you can't use <code>mem::uninitialized</code> safely, with generics</p>



<a name="134030761"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030761" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030761">(Sep 15 2018 at 23:52)</a>:</h4>
<p>and if you think you can, chances are you're wrong</p>



<a name="134030762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030762">(Sep 15 2018 at 23:52)</a>:</h4>
<p>there's a reason we're trying to deprecate it into oblivion</p>



<a name="134030771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134030771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134030771">(Sep 15 2018 at 23:53)</a>:</h4>
<p>(and <code>mem::zeroed</code> is almost entirely as bad)</p>



<a name="134108545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134108545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134108545">(Sep 17 2018 at 15:40)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> in terms of <span class="user-mention" data-user-id="120791">@RalfJ</span>'s blog post, this code is incorrect because the <strong>validity predicate</strong> (which must <em>always</em> hold, even in unsafe code) of <code>&amp;T</code> will require that it be non-null (though this is not "formally decided" or even discussed; but it's needed to justify the <code>Option</code>-layout optimizations)</p>



<a name="134115763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134115763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134115763">(Sep 17 2018 at 17:46)</a>:</h4>
<p><code>mem::uninitialized::&lt;&amp;()&gt;()</code> is insta-UB...</p>



<a name="134115871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134115871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134115871">(Sep 17 2018 at 17:48)</a>:</h4>
<blockquote>
<p>why is cloned needed to trigger it?</p>
</blockquote>
<p>this is a question you can never answer with UB. or rather, the answer always is "because otherwise LLVM was not yet smart enough to see youwere doing it wrong"</p>



<a name="134115896"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134115896" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134115896">(Sep 17 2018 at 17:49)</a>:</h4>
<p>only if you e.g. run this code in miri you can start doing actual statements of the form "adding X makes a difference"</p>



<a name="134115904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134115904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134115904">(Sep 17 2018 at 17:49)</a>:</h4>
<p>(though miri does not yet implement enough validation to notice the insta-UB in the first line here)</p>



<a name="134116301"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134116301" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134116301">(Sep 17 2018 at 17:55)</a>:</h4>
<p>But at least at this point we are close to having <code>MaybeUninit</code> in nightly</p>



<a name="134116310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best%20game%3A%20is%20this%20UB%3F/near/134116310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/best.20game.3A.20is.20this.20UB.3F.html#134116310">(Sep 17 2018 at 17:55)</a>:</h4>
<p>might just take another year or so tog et it on stable... ;)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>